<?php
namespace Admin\Controller;
class BaikeController extends IsloginController{
	function oper(){
		$baike = M("Baikelist");
		$num=$baike->count();
// 		3、规定每页显示的条数
		$pageSize=8;
// 		4、实例化分页类
		$pageOb=new \Think\Page($num,$pageSize);
// 		5、获取开始位置值
		$start=$pageOb->firstRow;
// 		6、获取分页结果
		$pageStr=$pageOb->show();
		$arr = $baike->limit("$start,$pageSize")->select();
		if($arr){
			$this->assign('pageStr',$pageStr);
			$this->assign('arr',$arr);
		}else{
			
		}
		$this->display();
	}
	function add(){
		$ob = M("Baike");
		$arr = $ob->select();
		$this->assign('arr',$arr);
		$this->display();
	}
	function tianjia(){
		$upload= new \Think\Upload();
		$upload->autoSub  =false;
		$upload->mimes=array('image/png',"image/gif","image/jpeg");
        $upload->maxSize =10000000000;
		$upload->rootPath = './Public';
       	$upload->savePath = '/Upload/';
        $upload->saveName = array('uniqid','');//文件名规则
        $upload->replace = true;//存在同名文件覆盖
		$info=$upload->upload();
		if($info){
            $image = new \Think\Image();
            foreach($info as $file) {
                //$file_mini='./Public/'.$file['savepath'].'mini/'.$file['savename'];
                $file_path="./Public/Upload/".$file['savePath'].$file['savename'];
                $file_mini="./Public/Upload/".$file['savePath'].$file['savename'];
                var_dump($file_path);
                var_dump($file_mini);
                $image = new \Think\Image();
                $image->open($file_path);
// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
                $image->thumb(140, 140)->save($file_mini);
            }
			$_POST['imagename']=$info['imagename']['savename'];
		}
		$ob = M("Baikelist");
		$_POST["addtime"] = time();
		$re = $ob->data($_POST)->add();
		if($re){
			$this->success("添加成功",U("Baike/oper"));
		}else{
			$this->error("添加失败",U("Baike/add"));
		}
	}
	function del(){
		$ob = M("Baikelist");
		$id = $_POST["id"];
		$re = $ob->where("id=$id")->delete();
		if($re){
			echo json_encode(array('msg'=>1));
		}

	}
	function update(){
	    $id=$_GET["id"];
        $ob = M("Baike");
        $arr = $ob->select();
        $this->assign('arr',$arr);
        $ob1 = M("Baikelist");
        $arr1 = $ob1->where("id=$id")->find();
        $this->assign("arr1",$arr1);
        $this->display();
    }
    function fabu(){
        $upload= new \Think\Upload();
        $upload->autoSub  =false;
        $upload->mimes=array('image/png',"image/gif","image/jpeg");
        $upload->rootPath = './Public';
        $upload->savePath = '/Upload/';
        $info=$upload->upload();
        if($info){
            $_POST['imagename']=$info['imagename']['savename'];
        }
        $Ob=M("Baikelist");

        $_POST['addtime']=time();
        $re = $Ob->data($_POST)->save();
        if($re){
            $this->success("修改成功",U('Baike/oper'));
        }else{
            $this->error("修改失败",U("Baike/oper"));
        }
    }

}